package com.oliet.controller;


import com.oliet.common.result.CommonResponse;
import com.oliet.common.vo.file.DownloadVO;
import com.oliet.common.vo.file.FileResourceVO;
import com.oliet.common.vo.page.PageVO;
import com.oliet.service.FileResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author JZP
 * @since 2023-12-22
 */
@RestController
@RequestMapping("/files")
public class FileResourcesController {

    @Autowired
    private FileResourcesService filesService;

    @Deprecated
    @PostMapping("/upload")
    public CommonResponse uploadFile(@RequestParam("file") MultipartFile file) {
        this.filesService.upload(file);
        return CommonResponse.success();
    }
    @Deprecated
    @PostMapping("/batchUpload")
    public CommonResponse batchUpload(@RequestParam("files") MultipartFile[] files) {
        this.filesService.batchUpload(files);
        return CommonResponse.success();
    }

    @PostMapping("/batchUploadOther")
    public CommonResponse batchUploadOther(@RequestParam("files") MultipartFile[] files,
                                           @RequestParam("classType") String classType,
                                           @RequestParam("fileType") String fileType,
                                           @RequestParam("displayPosition") String displayPosition) {
        this.filesService.batchUploadOther(files, classType, fileType, displayPosition);
        return CommonResponse.success();
    }

    @GetMapping("/download")
    public ResponseEntity<Resource> download(@RequestParam("id") Long id) throws IOException {
        DownloadVO result = this.filesService.download(id);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode(result.getFileName(), "UTF-8"));
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(result.getResource().contentLength())
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(result.getResource());
    }

    @DeleteMapping("/delete")
    public CommonResponse delete(@RequestParam Long id) {
        this.filesService.delete(id);
        return CommonResponse.success();
    }

    @GetMapping("/list")
    public CommonResponse list(@RequestParam(value = "pageNo", defaultValue = "1") Long pageNo,
                               @RequestParam(value = "pageSize", defaultValue = "20") Long pageSize,
                               @RequestParam(value = "classType", required = false) String classType,
                               @RequestParam(value = "fileType", required = false) String fileType,
                               @RequestParam(value = "displayPosition", required = false) String displayPosition,
                               @RequestParam(value = "search", required = false) String search) {
        PageVO<FileResourceVO> result = this.filesService.pageList(pageNo, pageSize, classType, fileType, displayPosition, search);
        return CommonResponse.data(result);
    }


}
